<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class Admin_BlogController extends Zend_Controller_Action
{

	public function init(){
		 $this->blogupdatemess="";
	}

    public function indexAction()
    {
       Zend_Loader::loadFile("../application/admin/models/Blog.php");
         $this->_helper->layout->setLayout('admin_layout');
         $request = $this->getRequest();
       $blog=new Blog();
       $blogarr=$blog->listblogs();
       $stat = $request->getParam('stat');
     //  print_r($blogarr);exit;
       $this->view->stat = $stat;
       $this->view->blogarr=$blogarr;
    }
       public function addAction()
    {
        Zend_Loader::loadFile('../application/admin/forms/Blog.php');
       $this->_helper->layout->setLayout('admin_layout');
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $blog_form = new Form_blogform($options);
        $this->view->blog_form = $blog_form;
        $this->view->title = "Edit";

    }
       public function editAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Blog.php");
        Zend_Loader::loadFile("../application/admin/forms/Blog.php");
        $blogmodel=new Blog();
        $this->_helper->layout->setLayout('admin_layout');
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $id = $this->_getParam('id', 0);
        $options = array("action"=>$action, "id"=>$id);
        $blogform = new Form_blogform($options);
        $this->view->blogform =  $blogform;
        $this->view->id = $id;
        if ($id > 0) {
            $blogform->populate($blogmodel->getblog($id));
        }      
        $blogdata=$blogmodel->getblog($id);
        $this->view->publish_up = !empty($blogdata["publish_up"])?$blogdata["publish_up"]:"";
        $this->view->publish_down = !empty($blogdata["publish_down"])?$blogdata["publish_down"]:"";
        $this->view->blogupdatemess =  $this->blogupdatemess;


    }

     public function doaddAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Blog.php");
        Zend_Loader::loadFile("../application/admin/forms/Blog.php");
      // print_r($_POST);
         $request = $this->getRequest();
        $modblog=new Blog();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $blogform=new Form_blogform($options);
        if($this->getRequest()->isPost()){
            $formdata = $this->getRequest()->getPost();
            if($blogform->isValid($formdata)){
                    $insertarr["title"]= preg_replace ( "/'/", "\\'", $request->getParam('title') );
                    $insertarr["alias"]=preg_replace ( "/'/", "\\'", $request->getParam('alias') );
                    $insertarr["full_text"]=preg_replace ( "/'/", "\\'", $request->getParam('full_text') );
                    $insertarr["catid"]=preg_replace ( "/'/", "\\'", $request->getParam('catid') );
                    $insertarr["created_by_alias"]=preg_replace ( "/'/", "\\'", $request->getParam('created_by_alias') );
                    $insertarr["state"]=preg_replace ( "/'/", "\\'",$request->getParam('state') );
                    $insertarr["publish_up"]=preg_replace ( "/'/", "\\'", $request->getParam('publish_up') );
                    $insertarr["publish_down"]=preg_replace ( "/'/", "\\'", $request->getParam('publish_down') );
                    $insertarr["access"]=preg_replace ( "/'/", "\\'", $request->getParam('access') );
                    $insertarr["metadesc"]=preg_replace ( "/'/", "\\'", $request->getParam('metadesc') );
                    $insertarr["metakey"]=preg_replace ( "/'/", "\\'", $request->getParam('metakey') );
                         $addblog=$modblog->addblog($insertarr)  ;
                    

                    if($addblog)
                    {
                         $this->_redirect('/admin/blog/index/stat/updated');
                         $this->blogupdatemess="Successfully added!";

                    }
              }
              else{
                   $blogform->populate($formdata);
                   $this->_helper->layout->setLayout('admin_layout');
                   $this->view->blogform = $blogform;
              }
            
            }
            
    }

    public function doeditAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Blog.php");
        Zend_Loader::loadFile("../application/admin/forms/Blog.php");
      // print_r($_POST);
        $modblog=new Blog();
        
        $request = $this->getRequest();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $blogform=new Form_blogform($options);
        
         if($this->getRequest()->isPost()){
            $formdata = $this->getRequest()->getPost();
            if($blogform->isValid($formdata)){
                $insertarr["id"]= preg_replace ( "/'/", "\\'", $request->getParam('id') );
                $insertarr["title"]= preg_replace ( "/'/", "\\'", $request->getParam('title') );
                $insertarr["alias"]=preg_replace ( "/'/", "\\'", $request->getParam('alias') );
                $insertarr["full_text"]=preg_replace ( "/'/", "\\'", $request->getParam('full_text') );
                $insertarr["catid"]=preg_replace ( "/'/", "\\'", $request->getParam('catid') );
                $insertarr["created_by_alias"]=preg_replace ( "/'/", "\\'", $request->getParam('created_by_alias') );
                $insertarr["state"]=preg_replace ( "/'/", "\\'",$request->getParam('state') );
                $insertarr["publish_up"]=preg_replace ( "/'/", "\\'", $request->getParam('publish_up') );
                $insertarr["publish_down"]=preg_replace ( "/'/", "\\'", $request->getParam('publish_down') );
                $insertarr["access"]=preg_replace ( "/'/", "\\'", $request->getParam('access') );
                $insertarr["metadesc"]=preg_replace ( "/'/", "\\'", $request->getParam('metadesc') );
                $insertarr["metakey"]=preg_replace ( "/'/", "\\'", $request->getParam('metakey') );
                $editblog=$modblog->editblog($insertarr)  ;
                if($editblog)
                {
                     //$this->_redirect('admin/blog/edit/id/'.$insertarr["id"]);
                    $this->_redirect('admin/blog/index/stat/updated');
                    $this->blogupdatemess="Successfully updated!";
                }
            }
            else{
                $blogform->populate($formdata);
                $this->_helper->layout->setLayout('admin_layout');
                $this->view->blogform = $blogform;
            }

            
         }
        
    }

     public function deleteAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Blog.php");
      // print_r($_POST);
        $modblog=new Blog();
        $request = $this->getRequest();
        $id=$request->getParam('id');
        $deleteblog=$modblog->deleteblog($id);
        if($deleteblog)
        {
             $this->_redirect('admin/blog/index/stat/deleted');
        }
     }
     public function changestatusAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Blog.php");
		$modBlog = new Blog();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$modBlog->changeBlogStatus($id);
	}

   public function getcontentAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Blog.php");
		$modBlog = new Blog();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		echo $modBlog->getcontent($id);
	}




}
?>
